home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH12 / EX12_2.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-02-22  |  6.8 KB  |  409 lines

  1. ; EX12_2.asm
  2. ;
  3. ; Program to demonstrate the ENTER and LEAVE instructions in Chapter 12.
  4. ;
  5. ; This program simulates the following Pascal code:
  6. ;
  7. ; program EnterLeave;
  8. ; var i:integer;
  9. ;
  10. ;    procedure Lex1;
  11. ;    var j:integer;
  12. ;
  13. ;    procedure Lex2;
  14. ;    var k:integer;
  15. ;
  16. ;        procedure Lex3;
  17. ;        var m:integer;
  18. ;
  19. ;        procedure Lex4;
  20. ;        var n:integer;
  21. ;        begin
  22. ;
  23. ;            writeln('Lex4');
  24. ;            for i:= 0 to 3 do
  25. ;                for j:= 0 to 2 do
  26. ;                write('(',i,',',j,') ');
  27. ;            writeln;
  28. ;            for k:= 1 downto 0 do
  29. ;              for m:= 1 downto 0 do
  30. ;                for n := 0 to 1 do
  31. ;                write('(',m,',',k,',',n,') ');
  32. ;            writeln;
  33. ;        end;
  34. ;
  35. ;        begin {Lex3}
  36. ;
  37. ;        writeln('Lex3');
  38. ;        for i := 0 to 1 do
  39. ;            for j := 0 to 1 do
  40. ;            for k := 0 to 1 do
  41. ;                for m := 0 to 1 do
  42. ;                writeln(i,j,k,m);
  43. ;
  44. ;        Lex4;
  45. ;
  46. ;        end; {Lex3}
  47. ;
  48. ;    begin {Lex2}
  49. ;
  50. ;        writeln('Lex2');
  51. ;        for i := 1 downto 0 do
  52. ;        for j := 0 to 1 do
  53. ;            for k := 1 downto 0 do
  54. ;            write(i,j,k,' ');
  55. ;        writeln;
  56. ;
  57. ;        Lex3;
  58. ;
  59. ;    end; {Lex2}
  60. ;
  61. ;    begin {Lex1}
  62. ;
  63. ;    writeln('Lex1');
  64. ;    Lex2;
  65. ;
  66. ;    end; {Lex1}
  67. ;
  68. ; begin {Main (lex0)}
  69. ;
  70. ;    writeln('Main Program');
  71. ;    Lex1;
  72. ;
  73. ; end.
  74.         .xlist
  75.         include     stdlib.a
  76.         includelib    stdlib.lib
  77.         .list
  78.  
  79.         .286                ;Allow ENTER & LEAVE.
  80.  
  81. ; Common equates all the procedures use:
  82.  
  83. wp        textequ    <word ptr>
  84. disp1        textequ    <word ptr [bp-2]>
  85. disp2        textequ    <word ptr [bp-4]>
  86. disp3        textequ    <word ptr [bp-6]>
  87.  
  88. ; Note: the data segment and the stack segment are one and the same in this
  89. ; program.  This is done to allow the use of the [bx] addressing mode when
  90. ; referencing local and intermediate variables without having to use a
  91. ; stack segment prefix.
  92.  
  93. sseg        segment    para stack 'stack'
  94.  
  95. i        word    ?            ;Main program variable.
  96.  
  97. stk        word    2046 dup (0)
  98.  
  99. sseg        ends
  100.  
  101.  
  102.  
  103. cseg        segment    para public 'code'
  104.         assume    cs:cseg, ds:sseg, ss:sseg
  105.  
  106.  
  107. ; Main's activation record looks like this:
  108. ;
  109. ;    | return address |<- SP, BP
  110. ;    |----------------|
  111.  
  112.  
  113. Main        proc
  114.         mov    ax, ss        ;Make SS=DS to simplify addressing (there
  115.         mov    ds, ax        ; will be no need to stick "SS:" in front
  116.         mov    es, ax        ; of addressing modes like "[bx]").
  117.  
  118.         print
  119.         byte    "Main Program",cr,lf,0
  120.         call    Lex1
  121. Quit:        ExitPgm            ;DOS macro to quit program.
  122. Main        endp
  123.  
  124.  
  125.  
  126. ; Lex1's activation record looks like this:
  127. ;
  128. ;    | return address |
  129. ;    |----------------|
  130. ;    | Dynamic Link   | <- BP
  131. ;    |----------------|
  132. ;    | Lex1's AR Ptr  |  | Display
  133. ;    |----------------|
  134. ;    | J Local var    | <- SP  (BP-4)
  135. ;    |----------------|
  136.  
  137. Lex1_J        textequ    <word ptr [bx-4]>
  138.  
  139. Lex1        proc    near
  140.         enter    2, 1        ;A 2 byte local variable at lex level 1.
  141.  
  142.         nop            ;Spacer instruction for single stepping.
  143.  
  144.         print
  145.         byte    "Lex1",cr,lf,0
  146.         call    Lex2
  147.         leave
  148.         ret
  149. Lex1        endp
  150.  
  151.  
  152.  
  153. ; Lex2's activation record looks like this:
  154. ;
  155. ;    | return address |
  156. ;    |----------------|
  157. ;    | Dynamic Link   | <- BP
  158. ;    |----------------|
  159. ;    | Lex1's AR Ptr  |  |
  160. ;    |----------------|  | Display
  161. ;    | Lex2's AR Ptr  |  |
  162. ;    |----------------|
  163. ;    | K Local var    | <- SP (BP-6)
  164. ;    |----------------|
  165. ;
  166. ;        writeln('Lex2');
  167. ;        for i := 1 downto 0 do
  168. ;        for j := 0 to 1 do
  169. ;            for k := 1 downto 0 do
  170. ;            write(i,j,k,' ');
  171. ;        writeln;
  172. ;
  173. ;        Lex3;
  174.  
  175. Lex2_k        textequ    <word ptr [bx-6]>
  176. k        textequ    <word ptr [bp-6]>
  177.  
  178. Lex2        proc    near
  179.         enter    2, 2        ;A 2-byte local variable at lex level 2.
  180.  
  181.         nop            ;Spacer instruction for single stepping.
  182.  
  183.         print
  184.         byte    "Lex2",cr,lf,0
  185.  
  186.         mov    i, 1
  187. ForLpI:        mov    bx, disp1    ;"J" is at lex level one.
  188.         mov    Lex1_J, 0
  189. ForLpJ:        mov    k, 1        ;"K" is local.
  190.  
  191. ForLpK:        mov    ax, i
  192.         puti
  193.         mov    bx, disp1
  194.         mov    ax, Lex1_J
  195.         puti
  196.         mov    ax, k
  197.         puti
  198.         mov    al, ' '
  199.         putc
  200.  
  201.         dec    k        ;Decrement from 1->0 and quit
  202.         jns    ForLpK        ; if we hit -1.
  203.  
  204.         mov    bx, disp1
  205.         inc    Lex1_J
  206.         cmp    Lex1_J, 2
  207.         jb    ForLpJ
  208.  
  209.         dec    i
  210.         jns    ForLpI
  211.  
  212.         putcr
  213.         call    Lex3
  214.  
  215.         leave
  216.         ret
  217. Lex2        endp
  218.  
  219.         
  220.  
  221. ; Lex3's activation record looks like this:
  222. ;
  223. ;    | return address |
  224. ;    |----------------|
  225. ;    | Dynamic Link   | <- BP
  226. ;    |----------------|
  227. ;    | Lex1's AR Ptr  |  |
  228. ;    |----------------|  | 
  229. ;    | Lex2's AR Ptr  |  | Display
  230. ;    |----------------|  |
  231. ;    | Lex3's AR Ptr  |  |
  232. ;    |----------------|
  233. ;    | M Local var    | <- SP (BP-8)
  234. ;    |----------------|
  235. ;
  236. ;        writeln('Lex3');
  237. ;        for i := 0 to 1 do
  238. ;            for j := 0 to 1 do
  239. ;            for k := 0 to 1 do
  240. ;                for m := 0 to 1 do
  241. ;                writeln(i,j,k,m);
  242. ;
  243. ;        Lex4;
  244. ;
  245.  
  246. Lex3_M        textequ    <word ptr [bx-8]>
  247. m        textequ    <word ptr [bp-8]>
  248.  
  249. Lex3        proc    near
  250.         enter    2, 3        ;2-byte variable at lex level 3.
  251.  
  252.         nop            ;Spacer instruction for single stepping.
  253.  
  254.         print
  255.         byte    "Lex3",cr,lf,0
  256.  
  257.         mov    i, 0
  258. ForILp:        mov    bx, disp1
  259.         mov    Lex1_J, 0
  260. ForJlp:        mov    bx, disp2
  261.         mov    Lex2_K, 0
  262. ForKLp:        mov    m, 0
  263. ForMLp:        mov    ax, i
  264.         puti
  265.         mov    bx, disp1
  266.         mov    ax, Lex1_J
  267.         puti
  268.         mov    bx, disp2
  269.         mov    ax, Lex2_k
  270.         puti
  271.         mov    ax, m
  272.         puti
  273.         putcr
  274.  
  275.         inc    m
  276.         cmp    m, 2
  277.         jb    ForMLp
  278.  
  279.         mov    bx, disp2
  280.         inc    Lex2_K
  281.         cmp    Lex2_K, 2
  282.         jb    ForKLp
  283.  
  284.         mov    bx, disp1
  285.         inc    Lex1_J
  286.         cmp    Lex1_J, 2
  287.         jb    ForJLp
  288.  
  289.         inc    i
  290.         cmp    i, 2
  291.         jb    ForILp
  292.  
  293.         call    Lex4
  294.  
  295.         leave
  296.         ret
  297. Lex3        endp
  298.  
  299.  
  300. ; Lex4's activation record looks like this:
  301. ;
  302. ;    | return address |
  303. ;    |----------------|
  304. ;    | Dynamic Link   | <- BP
  305. ;    |----------------|
  306. ;    | Lex1's AR Ptr  |  |
  307. ;    |----------------|  | 
  308. ;    | Lex2's AR Ptr  |  |
  309. ;    |----------------|  | Display
  310. ;    | Lex3's AR Ptr  |  |
  311. ;    |----------------|  |
  312. ;    | Lex4's AR Ptr  |  |
  313. ;    |----------------|
  314. ;    | N Local var    | <- SP (BP-10)
  315. ;    |----------------|
  316. ;
  317. ;
  318. ;            writeln('Lex4');
  319. ;            for i:= 0 to 3 do
  320. ;                for j:= 0 to 2 do
  321. ;                write('(',i,',',j,') ');
  322. ;            writeln;
  323. ;            for k:= 1 downto 0 do
  324. ;              for m:= 1 downto 0 do
  325. ;                for n := 0 to 1 do
  326. ;                write('(',m,',',k,',',n,') ');
  327. ;            writeln;
  328.  
  329. n        textequ    <word ptr [bp-10]>
  330.  
  331. Lex4        proc    near
  332.         enter    2, 4        ;2-byte local variable at lex level 4.
  333.  
  334.         nop            ;Spacer instruction for single stepping.
  335.  
  336.         print
  337.         byte    "Lex4",cr,lf,0
  338.  
  339.         mov    i, 0
  340. ForILp:        mov    bx, disp1
  341.         mov    Lex1_J, 0
  342. ForJLp:        mov    al, '('
  343.         putc
  344.         mov    ax, i
  345.         puti
  346.         mov    al, ','
  347.         putc
  348.         mov    ax, Lex1_J    ;Note that BX still contains disp1.
  349.         puti
  350.         print
  351.         byte    ") ",0
  352.  
  353.         inc    Lex1_J        ;BX still contains disp1.
  354.         cmp    Lex1_J, 3
  355.         jb    ForJLp
  356.  
  357.         inc    i
  358.         cmp    i, 4
  359.         jb    ForILp
  360.  
  361.         putcr
  362.  
  363.         mov    bx, disp2
  364.         mov    Lex2_K, 1
  365. ForKLp:        mov    bx, disp3
  366.         mov    Lex3_M, 1
  367. ForMLp:        mov    n, 0
  368. ForNLp:        mov    al, '('
  369.         putc
  370.  
  371.         mov    bx, disp3
  372.         mov    ax, Lex3_M
  373.         puti
  374.         mov    al, ','
  375.         putc
  376.         mov    bx, disp2
  377.         mov    ax, Lex2_K
  378.         puti
  379.         mov    al, ','
  380.         putc
  381.         mov    ax, n
  382.         puti
  383.         print
  384.         byte    ") ",0
  385.  
  386.         inc    n
  387.         cmp    n, 2
  388.         jb    ForNLp
  389.  
  390.         mov    bx, disp3
  391.         dec    Lex3_M
  392.         jns    ForMLp
  393.  
  394.         mov    bx, disp2
  395.         dec    Lex2_K
  396.         jns    ForKLp
  397.  
  398.         leave
  399.         ret
  400. Lex4        endp
  401.  
  402. cseg            ends
  403.  
  404.  
  405. zzzzzzseg    segment    para public 'zzzzzz'
  406. LastBytes    db    16 dup (?)
  407. zzzzzzseg    ends
  408.         end    Main
  409.